﻿using System;
using System.Web.UI;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Code;
using SBACN.WebSite.Common;
using SBACN.WebSite.Models;
using SBACN.WebSite.UC;

namespace SBACN.WebSite.Admin.Systems
{
	public partial class RightList : AdminBasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				this.InitCtrl();
			}
		}

		private void InitCtrl()
		{
			this.rpt_Right.DataSource = SystemMapCache.Instance.List;
			this.rpt_Right.DataBind();
		}

		protected void btn_Create_Click(object sender, EventArgs e)
		{
			int sortIndex = 0;
			if(string.IsNullOrEmpty(this.txt_Name.Text))
			{
				this.lbl_Msg.Text = "功能名称不能为空!";
				ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "$jqy(document).ready(function () {ShowBlock(false);});", true);
			}
			else if (string.IsNullOrEmpty(this.hid_RightCode.Value) && string.IsNullOrEmpty(this.txt_Code.Text))
			{
				this.lbl_Msg.Text = "系统代码不能为空!";
				ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "$jqy(document).ready(function () {ShowBlock(false);});", true);
			}
			else if (string.IsNullOrEmpty(this.txt_CText.Text))
			{
				this.lbl_Msg.Text = "中文显示不能为空!";
				ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "$jqy(document).ready(function () {ShowBlock(false);});", true);
			}
			else if (string.IsNullOrEmpty(this.txt_SortIndex.Text) || !int.TryParse(this.txt_SortIndex.Text, out sortIndex))
			{
				this.lbl_Msg.Text = "排列顺序输入不正确!";
				ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "$jqy(document).ready(function () {ShowBlock(false);});", true);
			}
			else
			{
				bool IsCreateNew = !string.IsNullOrWhiteSpace(this.txt_Code.Text);

				TB_SystemMap map = SystemMapCache.Instance[string.IsNullOrWhiteSpace(this.hid_RightCode.Value) ? this.txt_Code.Text.Trim() : this.hid_RightCode.Value.Trim()];

				if(IsCreateNew && map != null)
				{
					this.lbl_Msg.Text = "该功能的系统代码已经存在!";
					ClientScript.RegisterClientScriptBlock(this.GetType(), "Msg", "$jqy(document).ready(function () {ShowBlock(false);});", true);
				}
				else 
				{
					if (!IsCreateNew)//Edit
					{
						map.Name = this.txt_Name.Text.Trim();
						map.CText = this.txt_CText.Text.Trim();
						map.CURL = this.txt_CURL.Text.Trim();
						map.Description = this.txt_Memo.Text.Trim();
						map.EText = this.txt_EText.Text.Trim();
						map.EURL = this.txt_EURL.Text.Trim();
						map.AdminURL = this.txt_URL.Text.Trim();
						map.IsAdminRight = this.chb_IsAdmin.Checked;
						map.ShowInMap = this.chb_ShowInMap.Checked;
						map.SortIndex = sortIndex;
						map.Status = this.chb_Status.Checked;
						map.OpenInNewWindow = this.chb_OpenInNewWindow.Checked;
						map.NeedMemberLogin = this.chb_NeedLogin.Checked;
						DAL.DALHelper.Instance.UpdateWithTransaction<TB_SystemMap>(map, "Code");
					}
					else//CreateNew
					{
						map = DAL.DALHelper.Instance.Create<TB_SystemMap>();
						map.Name = this.txt_Name.Text.Trim();
						map.Code = this.txt_Code.Text.Trim();
						map.CText = this.txt_CText.Text.Trim();
						map.CURL = this.txt_CURL.Text.Trim();
						map.Description = this.txt_Memo.Text.Trim();
						map.EText = this.txt_EText.Text.Trim();
						map.EURL = this.txt_EURL.Text.Trim();
						map.AdminURL = this.txt_URL.Text.Trim();
						map.IsAdminRight = this.chb_IsAdmin.Checked;
						map.ShowInMap = this.chb_ShowInMap.Checked;
						map.SortIndex = sortIndex;
						map.Status = this.chb_Status.Checked;
						map.OpenInNewWindow = this.chb_OpenInNewWindow.Checked;
						map.NeedMemberLogin = this.chb_NeedLogin.Checked;
						DAL.DALHelper.Instance.Add(map);
					}
					DAL.DALHelper.Instance.SaveChanges();
					SystemMapCache.Instance.Clear();
					AdminMenu menu = this.Master.FindControl("AdminMenu1") as AdminMenu;
					if(menu != null)
					{
						menu.InitCtrl();
					}
				}
			}
			this.InitCtrl();
		}

		protected void btn_Delete_Click(object sender, EventArgs e)
		{
			if (!string.IsNullOrWhiteSpace(this.hid_RightCode.Value))
			{
				string code = this.hid_RightCode.Value.Trim();
				TB_SystemMap entity = SystemMapCache.Instance[code];
				entity.Status = !entity.Status;
				DAL.DALHelper.Instance.UpdateWithTransaction<TB_SystemMap>(entity, "Code");
				DAL.DALHelper.Instance.SaveChanges();
				SystemMapCache.Instance.Clear();
				this.InitCtrl();
				AdminMenu menu = this.Master.FindControl("AdminMenu1") as AdminMenu;
				if (menu != null)
				{
					menu.InitCtrl();
				}
			}
		}
	}
}